﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.Model;

namespace iLangl.BusinessModel.DAL.XMLStorage
{
	public class ContentDAL : BaseStorageDAL, IContentDAL
	{
		#region IContentDAL Members

		public void CreateContent(Model.ContentEntity content, System.Data.IDbTransaction transaction)
		{
			dtoXmlStorage.ContentRow contentRow = Storage.Content.FindById(content.Id);

			bool isNew = (contentRow == null);
			bool isChanged = false;

			if (isNew)
			{
				contentRow = Storage.Content.NewContentRow();
				isChanged = true;
			}

			if (!isChanged)
			{
				
				isChanged |= !content.Id.Equals(contentRow.Id);
				if (!contentRow.IsContentNull())
				isChanged |= !content.Content.Equals(contentRow.Content);
				if (content.Language != null)
				{
					if (!contentRow.IsLanguageCodeNull())
					isChanged |= !content.Language.LanguageId.Equals(contentRow.LanguageCode);
				}

				if (!contentRow.IsNameNull())
				isChanged |= !content.Name.Equals(contentRow.Name);

				if (content.Project != null)
				{
					if (!contentRow.IsProjectIdNull())
					isChanged |= !content.Project.Id.Equals(contentRow.ProjectId);
				}
				if (!contentRow.IsTypeNull())
				isChanged |= !content.Type.Equals(contentRow.Type);
				if (!contentRow.IsVersionNull())
				isChanged |= !content.Version.Equals(contentRow.Version);
				if (!contentRow.IsCustomerIdentityNull())
				isChanged |= !content.CustomerIdentity.Equals(contentRow.CustomerIdentity);
				if (!contentRow.IsTPIdentityNull())
				isChanged |= !content.TPIdentity.Equals(contentRow.TPIdentity);
			}

			contentRow.Id = content.Id;
			contentRow.Content = content.Content;
			
			if(content.Language != null)
			contentRow.LanguageCode = content.Language.LanguageId;

			contentRow.Name = content.Name;
			if(content.Project != null)
				contentRow.ProjectId = content.Project.Id;

			contentRow.Type = content.Type;
			contentRow.Version = content.Version;

			contentRow.CustomerIdentity = content.CustomerIdentity;
			contentRow.TPIdentity = content.TPIdentity;


			if (isNew)
			{
				Storage.Content.AddContentRow(contentRow);
			}
			if (isChanged)
			{
				contentRow.TimeSpan = CurrentTimeSpan;
			}

			if (transaction == null)
			{
				AcceptChanges();
			}
		}

		public IList<ContentEntity> ReadContentByProject(Project project)
		{
			List<ContentEntity> result = new List<ContentEntity>();

			foreach (dtoXmlStorage.ContentRow contentrow in Storage.Content)
			{
				if (string.Compare(contentrow.ProjectId, project.Id, true) == 0)
				{

					ContentEntity content = ReadContent(contentrow.Id);
					
					content.Project = project;

					result.Add(content);
				}
			}

			return result;
			
		}

		public ContentEntity ReadContent(string contentId)
		{
			dtoXmlStorage.ContentRow contentrow = Storage.Content.FindById(contentId);
			ContentEntity content = new ContentEntity();
			content.Id = contentrow.Id;
			content.Language = DALFacade.Instance.LanguageDAL.ReadLanguage(contentrow.LanguageCode);
			content.Content = contentrow.Content;
			content.Name = contentrow.Name;
			content.Type = contentrow.Type;
			content.Version = contentrow.Version;

			content.Project = Facade.Instance.ModelFactory.CreateProject();
			content.Project.Id = contentrow.ProjectId;

			if (!contentrow.IsCustomerIdentityNull())
				content.CustomerIdentity = contentrow.CustomerIdentity;

			if (!contentrow.IsTPIdentityNull())
				content.TPIdentity = contentrow.TPIdentity;

			return content;

		}
		
		public void UpdateContent(ContentEntity content, System.Data.IDbTransaction transaction)
		{
			CreateContent(content, transaction);
		}

		#endregion
	}
}
